
module fft_3d

  use dimensions
  use fft_wrap, only   : fft_forward, fft_backward


  complex, private   :: dummy1Dx(jx)
  complex, private   :: dummy1Dy(jy)
  complex, private   :: dummy1Dz(jz)


contains
!-------------------------------------------------------------------------------------
subroutine  fft_forward_x(arr3D)
!-------------------------------------------------------------------------------------

  implicit none

  complex, intent(inout) :: arr3D(jz,jy,jx)
  integer                :: ix, iy, iz

  do iy=1,jy
     do iz=1,jz 
        
        dummy1Dx = arr3D(iz,iy,:)
        call fft_forward(jx, dummy1Dx, dummy1Dx)
        dummy1Dx = dummy1Dx/float(jx)
        call dealias(jx,dummy1Dx)
        arr3D(iz,iy,:) = dummy1Dx(:)

     end do
  enddo

end subroutine fft_forward_x

!-------------------------------------------------------------------------------------
subroutine  fft_backward_x(arr3D)
!-------------------------------------------------------------------------------------

  implicit none

  complex, intent(inout) :: arr3D(jz,jy,jx)
  integer                :: ix, iy, iz

  do iy=1,jy
     do iz=1,jz 

        dummy1Dx = arr3D(iz,iy,:)
        call fft_backward(jx, dummy1Dx, dummy1Dx)
        arr3D(iz,iy,:) = dummy1Dx(:)

     end do
  enddo

end subroutine fft_backward_x

!-------------------------------------------------------------------------------------
subroutine  fft_forward_y(arr3D)
!-------------------------------------------------------------------------------------

  implicit none

  complex, intent(inout) :: arr3D(jz,jy,jx)
  integer                :: ix, iy, iz

  do ix=1,jx
     do iz=1,jz 
        
        dummy1Dy = arr3D(iz,:,ix)
        call fft_forward(jy, dummy1Dy, dummy1Dy)
        dummy1Dy = dummy1Dy/float(jy)
        call dealias(jy,dummy1Dy)
        arr3D(iz,:,ix) = dummy1Dy(:)

     end do
  enddo

end subroutine fft_forward_y

!-------------------------------------------------------------------------------------
subroutine  fft_backward_y(arr3D)
!-------------------------------------------------------------------------------------

  implicit none

  complex, intent(inout) :: arr3D(jz,jy,jx)
  integer                :: ix, iy, iz

  do ix=1,jx
     do iz=1,jz 

        dummy1Dy = arr3D(iz,:,ix)
        call fft_backward(jy, dummy1Dy, dummy1Dy)
        arr3D(iz,:,ix) = dummy1Dy(:)

     end do
  enddo

end subroutine fft_backward_y

!-------------------------------------------------------------------------------------
subroutine  fft_forward_z(arr3D)
!-------------------------------------------------------------------------------------

  implicit none

  complex, intent(inout) :: arr3D(jz,jy,jx)
  integer                :: ix, iy, iz

  do ix=1,jx
     do iy=1,jy 
        
        dummy1Dz = arr3D(:,iy,ix)
        call fft_forward(jz, dummy1Dz, dummy1Dz)
        dummy1Dz = dummy1Dz/float(jz)
        call dealias(jz,dummy1Dz)
        arr3D(:,iy,ix) = dummy1Dz(:)

     end do
  enddo

end subroutine fft_forward_z

!-------------------------------------------------------------------------------------
subroutine  fft_backward_z(arr3D)
!-------------------------------------------------------------------------------------

  implicit none

  complex, intent(inout) :: arr3D(jz,jy,jx)
  integer                :: ix, iy, iz

  do ix=1,jx
     do iy=1,jy 

        dummy1Dz = arr3D(:,iy,ix)
        call fft_backward(jz, dummy1Dz, dummy1Dz)
        arr3D(:,iy,ix) = dummy1Dz(:)

     end do
  enddo

end subroutine fft_backward_z



!-------------------------------------------------------------------------------------

end module fft_3d
